<?php
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/scripts/fileuploader/fileuploader.js', CClientScript::POS_HEAD);
Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl . '/scripts/fileuploader/fileuploader.css');
?>
<div class="form">

    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'posts-form',
        'enableAjaxValidation' => true,
            ));
    ?>


    <?php echo $form->errorSummary($model); ?>
    <fieldset>
        <legend>基本信息</legend>
        <div class="row clear">
            <?php echo $form->labelEx($model, 'category_id'); ?>
            <?php echo $category->category_name;?>
            <?php echo $form->hiddenField($model, 'category_id', array('value' => $category->id)); ?>
            <?php echo $form->error($model, 'category_id'); ?>
        </div>
        <div class="row input-float">
            <?php echo $form->labelEx($model, 'post_model'); ?>
            <?php echo $form->dropDownList($model, 'post_model', $model->post_model_array); ?>
            <?php echo $form->error($model, 'post_model'); ?>
        </div>
        <div class="row input-f20">
            <?php echo $form->labelEx($model, 'post_date'); ?>
            <?php
            //echo $form->textField($model, 'post_date');
            $this->widget('ext.CJuiDateTimePicker.CJuiDateTimePicker', array(
                'model' => $model, //Model object
                'attribute' => 'post_date', //attribute name
                'value' => $model->post_date,
                'mode' => 'datetime', //use "time","date" or "datetime" (default)
                'options' => array("dateFormat" => 'yy-mm-dd'), // jquery plugin options
                'language' => 'zh-CN',
            ));
            ?>
            <?php echo $form->error($model, 'post_date'); ?>
        </div>
        <div class="row input-f20">
            <?php echo $form->labelEx($model, 'post_status'); ?>
            <?php echo $form->dropDownList($model, 'post_status', $model->status_array); ?>
            <?php echo $form->error($model, 'post_status'); ?>
        </div>
        <div class="row input-f20">
            <?php echo $form->labelEx($model, 'comment_status'); ?>
            <?php echo $form->dropDownList($model, 'comment_status', $model->comment_status_array); ?>
            <?php echo $form->error($model, 'comment_status'); ?>
        </div>
        <div class="row clear">
            <?php echo $form->labelEx($model, 'post_author'); ?>
            <?php echo $form->textField($model, 'post_author', array('size' => 60, 'maxlength' => 255)); ?>
            <?php echo $form->error($model, 'post_author'); ?>
        </div>
    </fieldset>

    <fieldset>
        <legend>详细信息</legend>
        <div class="row">
            <?php echo $form->labelEx($model, 'post_title'); ?>
            <?php echo $form->textField($model, 'post_title', array('size' => 60, 'maxlength' => 255)); ?>
            <?php echo $form->error($model, 'post_title'); ?>
        </div>
        <div class="row">
            <?php echo $form->labelEx($model, 'post_alias'); ?>
            <?php echo $form->textField($model, 'post_alias', array('size' => 60, 'maxlength' => 255)); ?>
            <?php echo $form->error($model, 'post_alias'); ?>
        </div>
        <div class="row">
            <?php echo $form->labelEx($model, 'post_content'); ?>
            <?php
            echo $form->textArea($model, 'post_content', array('rows' => 30, 'cols' => 94));
            $this->widget('ext.kindeditor.KindEditor', array(
                'model' => $model,
                'attribute' => 'post_content',
                    )
            );
            ?>

            <?php echo $form->error($model, 'post_content'); ?>
        </div>
    </fieldset>

    <fieldset>
        <legend>SEO</legend>
        <div class="row">
            <?php echo $form->labelEx($model, 'seo_title'); ?>
            <?php echo $form->textField($model, 'seo_title', array('size' => 60, 'maxlength' => 255)); ?>
            <?php echo $form->error($model, 'seo_title'); ?>
        </div>

        <div class="row">
            <?php echo $form->labelEx($model, 'seo_keywords'); ?>
            <?php echo $form->textField($model, 'seo_keywords', array('size' => 60, 'maxlength' => 255)); ?>
            <?php echo $form->error($model, 'seo_keywords'); ?>
        </div>

        <div class="row">
            <?php echo $form->labelEx($model, 'seo_description'); ?>
            <?php echo $form->textArea($model, 'seo_description', array('rows' => 6, 'cols' => 90)); ?>
            <?php echo $form->error($model, 'seo_description'); ?>
        </div>
    </fieldset>
    <fieldset>
        <legend>相关附件</legend>
        <div class="row">
            <?php //echo $form->labelEx($model, 'post_resources'); ?>
            <?php 
            //echo $form->textArea($model, 'post_resources', array('rows' => 6, 'cols' => 90)); 
            if($model->id)
            {
                echo '<ul>';
                foreach(PostResources::model()->findAll("post_id=$model->id") as $val)
                {
                    $link = CHtml::link($val->file_name, Yii::app()->baseUrl.'/'.$val->file_url, array('target' => '_blank'));
                    echo "<li>$link <a href='javascript:void(0)' onclick='deleteFile($val->id, this)'>删除</a></li>";

                }
                echo '</ul>';
            }
            ?>
            <ul id="post_resources_box"></ul>
            <?php echo $form->error($model, 'post_resources'); ?>
            <div id="upload-thum">
                <noscript>          
                <p>Please enable JavaScript to use file uploader.</p>
                </noscript>   
            </div>
        </div>
    </fieldset>
    <fieldset class="tblFooters">
        <div class="row buttons">
            <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
        </div>
    </fieldset>


    <?php $this->endWidget(); ?>

</div><!-- form -->
<script>
    //delete file
    function deleteFile(id, obj){
        $.post("<?php echo $this->createUrl('ajax');?>", {action:"deleteResource", id:id}, function(data){
            if(data == 1){
                $(obj).parent("li").remove();
            }
            
        });
    }
    //delete upload file
    function deleteUpload(obj){
        $(obj).parent("li").remove();
    }
    //file upload
    var upimg='';
    var img_ext = "|jpg|jpeg|png|gif|bmp|doc|docx|xls|xlsx|";
    function createUploader(elementObj, setValueObj){//   elementObj: DIV, setValueObj: Will set value INPUT    
        var uploader = new qq.FileUploader({
            element: elementObj,
            action: '<?php echo Yii::app()->baseUrl; ?>/fileUpload.php',
            debug: false,
            showMessage: function(message){ alert(message); },
            allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'doc', 'docx','xls','xlsx'],
            onProgress: function(id, fileName, loaded, total){
                //location.reload();
            },
            onComplete:function(id, fileName, responseJSON){
                if(responseJSON.success){
                    var dot_index = responseJSON.url.lastIndexOf('.');
                    var ext = responseJSON.url.substring(dot_index+1);
                    if(img_ext.indexOf(ext)>0){
                        $("#post_resources_box").append("<li><input name='resourcesName[]' type='text' value='"+responseJSON.fileName+"' />:<input name='resourcesUrl[]' size='60' type='text' value='"+responseJSON.url+"' /> <a href='javascript:void(0)' onclick='deleteUpload(this)'>删除</a></li>");
                        //setValueObj.val(setValueObj.val() + "\n" + responseJSON.fileName + "|" + responseJSON.url);
                    }
                }
            }
        });
        uploader.setParams({dir:"posts"});
    }
    $(window).load(function(){
        createUploader(document.getElementById("upload-thum"), $("#Posts_post_resources"));
    });
</script>